<template>
  <!-- 监控管理 -->
  <div id="base">
    <!-- 搜索 -->
    <div class="search">
      <div class="searchTop">
        <el-form :inline="true" :model="search">
          <el-form-item label="设备编号">
            <el-input v-model="search.deviceCode" style="width:150px" placeholder="请输入设备编号"></el-input>
          </el-form-item>
          <el-form-item label="监控状态">
            <el-select
              style="width:100px"
              v-model="search.onlineStatus"
              placeholder="请选择"
              clearable
            >
              <el-option label="离线" value="0"></el-option>
              <el-option label="在线" value="1"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="站点名称">
            <el-select v-model="search.stationId" placeholder="请选择" style="width:120px" clearable>
              <el-option
                v-for="item in siteList"
                :key="item.id"
                :label="item.stationName"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="创建时间">
            <el-date-picker
              v-model="search.searchTime"
              type="datetimerange"
              value-format="yyyy-MM-dd HH:mm:ss"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
            ></el-date-picker>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="getAddBook">查询</el-button>
            <el-button @click="empty" type="info">清空</el-button>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="notice">新增</el-button>
            <!-- icon="el-icon-plus" -->
            <!-- <el-button icon="el-icon-upload2" class="buttonBotLast" @click="excellist">导出数据</el-button> -->
          </el-form-item>
        </el-form>
      </div>
    </div>

    <!-- 列表 -->
    <div id="table">
      <el-table
        :data="
          data.list.slice(
            (data.currpage - 1) * data.pagesize,
            data.currpage * data.pagesize
          )
        "
        stripe
        style="width: 100%;padding:0.8%;"
        :header-cell-style="{ background: 'rgba(237, 242, 246, 1)' }"
      >
        <el-table-column :index="table_index" type="index" label="序号"></el-table-column>
        <el-table-column prop="cameraNumber" label="设备编号"></el-table-column>
        <el-table-column prop="cameraName" label="设备名称"></el-table-column>
        <el-table-column prop="videoAddress" label="监控地址"></el-table-column>
        <el-table-column label="监控状态">
          <template slot-scope="scope">
            <span
              :class="scope.row.onlineStatus ==1?'green':'red'"
            >{{scope.row.onlineStatus ==1?'在线':'离线'}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="stationName" label="站点名称"></el-table-column>
        <el-table-column prop="updateTime" label="最后更新时间"></el-table-column>
        <el-table-column prop="createTime" label="创建时间"></el-table-column>
        <el-table-column prop="accessToken" label="token"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="primary" size="small" @click="showdetail(scope.row, scope.$index)">编辑</el-button>
            <el-button type="danger" size="small" @click="showrelease(scope.row, scope.$index)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 分页 -->
    <div class="pagination">
      <el-pagination
        :current-page="data.currpage"
        :page-size="data.pagesize"
        :page-sizes="[5, 10, 15, 20]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="data.list.length"
        @prev-click="nextpage"
        @next-click="nextpage"
        @current-change="nextpage"
        @size-change="sizeChange"
      ></el-pagination>
    </div>

    <!-- 新增/编辑 -->
    <el-dialog
      :title="addedittitle"
      width="450px"
      :visible.sync="showedit"
      @close="resetForm('ruleForm')"
      center
    >
      <markerPoint :region="region" @searchMarker="searchMarker" ref="child"></markerPoint>
      <el-form
        ref="ruleForm"
        :model="msg"
        :rules="rules"
        label-width="auto"
        class="msg"
        label-position="right"
        hide-required-asterisk
      >
        <el-form-item label="设备编号" prop="cameraNumber">
          <el-input v-model="msg.cameraNumber" :disabled="editDis" placeholder="请输入设备编号"></el-input>
        </el-form-item>
        <el-form-item label="设备名称" prop="cameraName">
          <el-input v-model="msg.cameraName" placeholder="请输入设备名称"></el-input>
        </el-form-item>
        <el-form-item label="监控地址" prop="videoAddress">
          <el-input v-model="msg.videoAddress" placeholder="请输入监控地址"></el-input>
        </el-form-item>
        <el-form-item label="token" prop="accessToken">
          <el-input v-model="msg.accessToken" placeholder="请输入token"></el-input>
        </el-form-item>
        
        <el-form-item label="站点名称" prop="stationId">
          <el-select
            v-model="msg.stationId"
            :disabled="editDis"
            placeholder="请选择站点名称"
            style="width:332px"
            clearable
          >
            <el-option
              v-for="item in siteList"
              :key="item.id"
              :label="item.stationName"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="resetForm('ruleForm')">取消</el-button>
        <el-button v-if="addedittitle == '监控设备管理-新增'" type="primary" @click="submitForm">保存</el-button>
        <el-button v-else type="primary" @click="editForm">保存</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import markerPoint from "../../components/makerPoint/makerPoint.vue";
export default {
  components: {
    markerPoint
  },
  data() {
    return {
      region: null,
      addedittitle: null,
      showedit: false,
      editDis: false,
      siteList: [],
      msg: {
        cameraNumber: "",
        cameraName: "",
        videoAddress: "",
        accessToken: "",
        stationId: ""
      },
      // 新建通知非空验证
      rules: {
        cameraNumber: [
          { required: true, message: "请输入设备编号", trigger: "blur" }
        ],
        cameraName: [
          {
            required: true,
            message: "请输入设备名称",
            trigger: "blur"
            // validator: hasAddressCode
          }
        ],
        videoAddress: [
          { required: true, message: "请输入监控地址", trigger: "blur" }
        ],
        accessToken: [
          { required: true, message: "请输入token码", trigger: "blur" }
        ],
        stationId: [
          { required: true, message: "请选择站点名称", trigger: "blur" }
        ]
      },
      // 搜索
      search: {
        deviceCode: "",
        onlineStatus: "",
        stationId: "",
        searchTime: []
      },
      // 列表数据
      data: {
        pagesize: 10,
        currpage: 1,
        list: []
      }
    };
  },
  created() {
    this.getList();
    if (localStorage.getItem("companyId") == 1) {
      this.region = "东营市";
    } else if (localStorage.getItem("companyId") == 2) {
      this.region = "滨州市";
    } else {
      this.region = "上绕市";
    }
    this.$http
      .post("deviceWater/findStation", {
        companyId: localStorage.getItem("companyId")
      })
      .then(res => {
        this.siteList = res.data;
      });
  },
  methods: {
    table_index(index) {
      return (this.data.currpage - 1) * this.data.pagesize + (index + 1);
    },
    //获取坐标
    searchMarker(point) {
      console.log(point);
      this.msg.address = point;
    },
    //获取地址坐标
    getMarkerPint() {
      this.$refs.child.callMethod();
    },
    //获取列表数据
    getList() {
      this.$http
        .post("deviceCamera/findList", {
          ...this.search,
          companyId: localStorage.getItem("companyId")
        })
        .then(res => {
          console.log(res);
          this.data.currpage = 1;
          this.data.list = res.data;
        });
    },
    //取消
    resetForm(formName) {
      if (localStorage.getItem("point")) {
        localStorage.removeItem("point");
      }
      this.$refs[formName].resetFields();
      this.showedit = false;
      this.msg = {};
    },

    //提交
    submitForm() {
      this.$refs["ruleForm"].validate(valid => {
        if (valid) {
          let {
            cameraNumber,
            cameraName,
            videoAddress,
            accessToken,
            stationId
          } = this.msg;
          console.log("新增");
          this.$http
            .post("deviceCamera/add", {
              cameraNumber,
              cameraName,
              videoAddress,
              accessToken,
              stationId,
              companyId: localStorage.getItem("companyId")
            })
            .then(res => {
              if (res.code == 1) {
                this.showedit = false;
                this.getList();
                this.$message({
                  type: "success",
                  showClose: true,
                  message: "新增成功",
                  offset: 155
                });
                this.msg = {};
              } else {
                this.$message({
                  type: "warning",
                  showClose: true,
                  message: res.message,
                  offset: 155
                });
              }
            });
        } else {
          return false;
        }
      });
    },

    editForm() {
      this.$refs["ruleForm"].validate(valid => {
        if (valid) {
          let { address, remarks, name, id } = this.msg;
          this.$http
            .post("deviceCamera/update", {
              ...this.msg,
              companyId: localStorage.getItem("companyId")
            })
            .then(res => {
              if (res.code == 1) {
                this.showedit = false;
                this.getList();
                this.$message({
                  type: "success",
                  showClose: true,
                  message: "编辑成功",
                  offset: 155
                });
              } else {
                this.$message({
                  type: "warning",
                  showClose: true,
                  message: res.message,
                  offset: 155
                });
              }
            });
        } else {
          return false;
        }
      });
    },
    //新增
    notice() {
      this.showedit = true;
      this.editDis = false;
      this.addedittitle = "监控设备管理-新增";
    },
    //编辑
    showdetail(row, index) {
      this.showedit = true;
      this.editDis = true;
      this.addedittitle = "监控设备管理-编辑";
      this.msg = JSON.parse(JSON.stringify(row));
    },
    //删除
    showrelease(row, index) {
      this.$confirm("此操作将删除此数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.$http
            .post("deviceCamera/deleteById", { id: row.id })
            .then(res => {
              // console.log(res);
              this.getList();
              this.$message({
                type: "success",
                message: res.message,
                offset: 155
              });
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    //查询
    getAddBook() {
      this.getList();
    },
    //清空
    empty() {
      this.search = {
        addressCode: "",
        name: "",
        deviceId: ""
      };
      this.getList();
    },
    // 翻页
    nextpage(value) {
      this.data.currpage = value;
    },
    // 显示条数切换
    sizeChange(total) {
      this.data.pagesize = total;
    }
    // 导出
    // excellist() {}
  }
};
</script>

<style lang="less" scoped>
.search {
  // overflow: hidden;
  display: flex;
  justify-content: space-between;
}
.table {
  width: 1128px;
  height: 465px;
  margin-top: 16px;
}
.table {
  width: 100%;
}

.tableButton3 {
  color: #fff;
  background-color: #f56c6c;
  border-radius: 3px;
}
.textmsg {
  color: red;
}
.textcc {
  color: #000;
}
.paginationList {
  text-align: center;
  margin-top: 32px;
  padding: 0;
}
.table {
  width: 1128px;
  height: 465px;
  margin-top: 16px;
}
.dialogText {
  .el-input {
    width: 240px;
    height: 32px;
  }
  .selectTop {
    width: 240px;
    height: 32px;
  }
  .selectBot {
    width: 240px;
    height: 32px;
  }
}
.formButon {
  width: 127px;
  height: 40px;
  text-align: center;
}

.pagination {
  text-align: center;
  margin-right: 25px;
  padding-top: 20px;
}
.green {
  color: green;
}
.red {
  color: red;
}
.dialog-footer {
  justify-content: center;
}
</style>
